/*********************************************************************
*                                                                    *
*                SEGGER Microcontroller GmbH & Co. KG                *
*        Solutions for real time microcontroller applications        *
*                                                                    *
**********************************************************************
*                                                                    *
* C-file generated by:                                               *
*                                                                    *
*        GUI_Builder for emWin version 5.26                          *
*        Compiled Aug  8 2014, 14:49:54                              *
*        (c) 2013 Segger Microcontroller GmbH & Co. KG               *
*                                                                    *
**********************************************************************
*                                                                    *
*        Internet: www.segger.com  Support: support@segger.com       *
*                                                                    *
**********************************************************************
*/

// USER START (Optionally insert additional includes)
// USER END

#include "DIALOG.h"
#include "DLG.h"
#include "light.h"

extern GUI_CONST_STORAGE GUI_FONT GUI_Fonticon24;
extern GUI_FONT GUI_Fontshuju_anniu40;
extern  GUI_FONT GUI_Fontsongit24;
/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_FRAMEWIN_0    (GUI_ID_USER + 0x00)
#define ID_BUTTON_0      (GUI_ID_USER + 0x01)
#define ID_BUTTON_1      (GUI_ID_USER + 0x02)
#define ID_BUTTON_2      (GUI_ID_USER + 0x03)
#define ID_BUTTON_3      (GUI_ID_USER + 0x04)
#define ID_BUTTON_4      (GUI_ID_USER + 0x05)
#define ID_TEXT_0        (GUI_ID_USER + 0x06)
#define ID_TEXT_1        (GUI_ID_USER + 0x07)
#define ID_TEXT_2        (GUI_ID_USER + 0x08)
#define ID_TEXT_3        (GUI_ID_USER + 0x09)
#define ID_TEXT_4        (GUI_ID_USER + 0x0A)

unsigned char LedFlag[4];




// USER START (Optionally insert additional defines)
// USER END

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

// USER START (Optionally insert additional static data)
// USER END

/*********************************************************************
*
*       _aDialogCreate
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
  { FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN_0, 0, 0, 800, 480, 0, 0x64, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_0, 145, 60, 140, 100, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_1, 145, 232, 140, 100, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_2, 622, 353, 159, 88, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_3, 540, 60, 140, 100, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_4, 540, 232, 140, 100, 0, 0x0, 0 },
  { TEXT_CreateIndirect, "Text", ID_TEXT_0, 4, 97, 135, 37, 0, 0x64, 0 },
  { TEXT_CreateIndirect, "Text", ID_TEXT_1, 5, 270, 134, 39, 0, 0x64, 0 },
  { TEXT_CreateIndirect, "Text", ID_TEXT_2, 378, 97, 146, 31, 0, 0x64, 0 },
  { TEXT_CreateIndirect, "Text", ID_TEXT_3, 383, 270, 135, 33, 0, 0x64, 0 },
		{ TEXT_CreateIndirect, "Text", ID_TEXT_4, 0, 0, 795, 35, 0, 0x0, 0 },
  // USER START (Optionally insert additional widgets)
  // USER END
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

// USER START (Optionally insert additional static code)
// USER END

/*********************************************************************
*
*       _cbDialog
*/
static void _cbDialog(WM_MESSAGE * pMsg) {
  WM_HWIN hItem;
  int     NCode;
  int     Id;
  // USER START (Optionally insert additional variables)
  // USER END

  switch (pMsg->MsgId) {
  case WM_INIT_DIALOG:
		/*****************************************************************************************
																					样式设置
		*****************************************************************************************/
    GUI_UC_SetEncodeUTF8();
	
		hItem = pMsg->hWin;
		FRAMEWIN_SetTitleVis(hItem,0);
	  hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_4);
    TEXT_SetFont(hItem, &GUI_Fontsongit24);
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
		TEXT_SetText(hItem,"灯光控制");
		TEXT_SetBkColor(hItem,0x000000);
		TEXT_SetTextColor(hItem,0xffffff);
		
			
		/* 每次进来这个界面都先读取LED的状态信息
		 * 根据现在的LED的状态信息来绘制按键是开还是关
	   */
		LedFlag[0] = ReadLight1Status();
		LedFlag[1] = ReadLight1Status();
		LedFlag[2] = ReadLight1Status();
		LedFlag[3] = ReadLight1Status();
	

    //BUTTON_SetFont(hItem, &GUI_Fonticon24);
    //
    // Initialization of 'Button'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1);
    BUTTON_SetFont(hItem, &GUI_Fonticon24);
		TEXT_SetTextColor(hItem,0xffffff);
    BUTTON_SetText(hItem, "开");
    //
    // Initialization of 'Button'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_2);
    BUTTON_SetFont(hItem, &GUI_Fontshuju_anniu40);
		TEXT_SetTextColor(hItem,0xffffff);
    BUTTON_SetText(hItem, "返回");
    //
    // Initialization of 'Button'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_3);
    BUTTON_SetFont(hItem, &GUI_Fonticon24);
		TEXT_SetTextColor(hItem,0xffffff);
    BUTTON_SetText(hItem, "开");
    //
    // Initialization of 'Button'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4);
    BUTTON_SetFont(hItem, &GUI_Fonticon24);
		TEXT_SetTextColor(hItem,0xffffff);
    BUTTON_SetText(hItem, "开");
    //
    // Initialization of 'Text'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
    TEXT_SetTextColor(hItem,0xffffff);
		TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
    TEXT_SetFont(hItem, &GUI_Fonticon24);
    TEXT_SetText(hItem, "主灯光:");
		
    //
    // Initialization of 'Text'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_1);
    TEXT_SetTextColor(hItem,0xffffff);
		TEXT_SetFont(hItem, &GUI_Fonticon24);
		TEXT_SetText(hItem, "副灯光:");
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
		
    //
    // Initialization of 'Text'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_2);
    TEXT_SetTextColor(hItem,0xffffff);
		TEXT_SetFont(hItem, &GUI_Fonticon24);
    TEXT_SetText(hItem, "氛围灯:");
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
		
    //
    // Initialization of 'Text'
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_3);
    TEXT_SetTextColor(hItem,0xffffff);
		TEXT_SetFont(hItem, &GUI_Fonticon24);
    TEXT_SetText(hItem, "氛围灯:");
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
	
		hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0);
		if(!LedFlag[0]){
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
		  BUTTON_SetText(hItem, "开");
		}else{
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
			 BUTTON_SetText(hItem, "关");
		}
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1);
		if(!LedFlag[1]){
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
		  BUTTON_SetText(hItem, "开");
		}else{
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
			 BUTTON_SetText(hItem, "关");
		}
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_3);
		if(!LedFlag[2]){
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
		  BUTTON_SetText(hItem, "开");
		}else{
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
			 BUTTON_SetText(hItem, "关");
		}
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4);
		if(!LedFlag[3]){
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
		  BUTTON_SetText(hItem, "开");
		}else{
			BUTTON_SetFont(hItem, &GUI_Fonticon24);
			 BUTTON_SetText(hItem, "关");
		}
		break;
	/*****************************************************************************************
	*****************************************************************************************/	
  case WM_NOTIFY_PARENT:
    //printf("D1:%d,D2:%d,D3:%d,D4:%d\r\n",LedFlag[0],LedFlag[1],LedFlag[2],LedFlag[3]);
		Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch(Id) {
    case ID_BUTTON_0: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
        /* 灯光控制按键被按下且松开 就打开或者关闭灯 
			   * 先读取LED的电平信息来判断灯有没有开
				 */
				LedFlag[0] = ReadLight1Status();
				if(!LedFlag[0]){
					Light1 = 1;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0), "关");
					
				}
				else{
					Light1 = 0;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0), "开");
				}
        break;
      }
      break;
    case ID_BUTTON_1: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
        		LedFlag[1] = ReadLight2Status();
				if(!LedFlag[1]){
					Light2 = 1;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), "关");
					
				}
				else{
					Light2 = 0;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), "开");
				}
        break;
      }
      break;
    case ID_BUTTON_2: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
        GUI_EndDialog(pMsg->hWin,0);
				CreateFramewin();
        break;
      }
      break;
    case ID_BUTTON_3: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
       		LedFlag[2] = ReadLight3Status();
				if(!LedFlag[2]){
					Light3 = 1;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_3), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_3), "关");
					
				}
				else{
					Light3 = 0;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_3), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_3), "开");
				}
        break;
      }
      break;
    case ID_BUTTON_4: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
      		LedFlag[3] = ReadLight4Status();
				if(!LedFlag[3]){
					Light4 = 1;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4), "关");
					
				}
				else{
					Light4 = 0;
					BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4), &GUI_Fonticon24);
					BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4), "开");
				}
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       CreateFramewin
*/

WM_HWIN CreateLightFramewin(void) {
  WM_HWIN hWin;

  hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);
  return hWin;
}


/*************************** End of file ****************************/
